/*
 * @Author: yayalee yangcheng960813@163.com
 * @Date: 2024-07-15 11:48:07
 * @LastEditTime: 2024-07-17 22:07:30
 * @LastEditors: yayalee yangcheng960813@163.com
 * @Description: 
 * @FilePath: \unfinished-challenges\assets\core_cc\widget\AdapterSize.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */

import { Component, UITransform, _decorator, log, math, view } from "cc";
import { EDITOR } from "cc/env";
import { _cc } from "../_cc";
import { EventName, eventsOnLoad, preloadEvent } from "../EventUtil";

const { ccclass, property, menu, executeInEditMode } = _decorator;

/**
 * - 适配组件，使节点与设计分辨率size保持一致，不影响节点位置
 * - 不使用Widget是因为某些需要改变节点position的情况下会产生冲突
 */
@ccclass("AdapterSize")
@eventsOnLoad()
@executeInEditMode
@menu("Core_cc/AdapterSize")
export default class AdapterSize extends Component {

    protected onLoad(): void {
        this.adapt();
    }

    @preloadEvent(EventName.RESIZE)
    public adapt(): void {
        if (EDITOR) {
            let size = view.getDesignResolutionSize();
            let transform = _cc.getCmpt(this.node, UITransform, true)!;
            if (transform) {
                transform.width = size.width;
                transform.height = size.height;
            }
        } else {
            let size = view.getVisibleSize();
            this.node.w = size.width;
            this.node.h = size.height;
        }
    }

    resetInEditor(): void {
        if (EDITOR) this.adapt();
    }
}
